<?php

/**
 * tableClient actions.
 *
 * @package    ProjetBar
 * @subpackage tableClient
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class tableClientActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
	$this->id_serveur = $request->getParameter('id_serveur');
	$this->id_table_client = $request->getParameter('id_table_client');

    $this->table_clients = Doctrine_Core::getTable('tableClient')
      ->createQuery('a')
      ->execute();
	  
    $this->table_clients_prete = Doctrine_Core::getTable('commandeClient')
		->createQuery('a')
		->where('a.id_etat_commande = 2')
		->execute();
	
	$requete_produit_commande_table_client_prets = Doctrine_Query::Create()
		->select('t.id_table_client as id_table_client, c.id_commande_client, pv.id_produit, pv.libelle_produit, pcc.quantite_produit, count(pv.id_produit) as nb_produit_pret')				
		->from('commandeClient c')
		->innerJoin('c.ProduitEnCommandeALaCarte pcc')		
		->innerJoin('pcc.EtatProduitEnVente e')
		->innerJoin('pcc.ProduitEnVente pv')
		->innerJoin('c.TableClient t')		
		->where('e.libelle_etat_produit_en_vente = :pret',array(':pret' => 'pret'));
		$this->produit_commande_table_client_prets = $requete_produit_commande_table_client_prets->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->table_client = Doctrine_Core::getTable('tableClient')->find(array($request->getParameter('id_table_client')));
    $this->forward404Unless($this->table_client);
  }

  public function executeNew(sfWebRequest $request)
  {
    $table_client = new tableClient();
	$table_client->save();
	$this->redirect('tableClient/index?id_serveur='.$request->getParameter('id_serveur').'');
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new tableClientForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($table_client = Doctrine_Core::getTable('tableClient')->find(array($request->getParameter('id_table_client'))), sprintf('Object table_client does not exist (%s).', $request->getParameter('id_table_client')));
    $this->form = new tableClientForm($table_client);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($table_client = Doctrine_Core::getTable('tableClient')->find(array($request->getParameter('id_table_client'))), sprintf('Object table_client does not exist (%s).', $request->getParameter('id_table_client')));
    $this->form = new tableClientForm($table_client);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($table_client = Doctrine_Core::getTable('tableClient')->find(array($request->getParameter('id_table_client'))), sprintf('Object table_client does not exist (%s).', $request->getParameter('id_table_client')));
    $table_client->delete();

    $this->redirect('tableClient/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $table_client = $form->save();

      $this->redirect('tableClient/edit?id_table_client='.$table_client->getIdTableClient());
    }
  }
}
